using JT808Protocol.Abstracts;
using JT808Protocol.Models;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace JT808Gateway.Core.Forwarders
{
    /// <summary>
    /// Tcp转发器
    /// </summary>
    public class TcpForwarder : IDataForwarder
    {
        public Task SendAsync(JT808Package package)
        {
            if (RefreshBindingsJob.TcpSessions.Count > 0)
            {
                if (RefreshBindingsJob.TcpSessions.TryGetValue(package.Header.SimNumber, out SocketAsyncEventArgs args))
                {
                    var socket = args.UserToken as Socket;

                    args.SetBuffer(package.OrginalData, 0, package.OrginalData.Length);
                    socket.SendAsync(args);
                }
            }

            return Task.CompletedTask;
        }
    }
}